package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.*;
import me.zhengjie.business.service.dto.*;
import me.zhengjie.pay.domain.PaidQueryCriteria;
import me.zhengjie.pay.service.UnPaidService;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "通用查询")
@RequestMapping("/api/generalQuery")
public class GeneralQueryController {

    private final GeneralQueryService generalQueryService;
    private final GsglService gsglService;
    private final UnPaidService unPaidService;
    private final SbgsService sbgsService;

    private final Kb04Service kb04Service;
    private final Kc21Service kc21Service;
    private final Kc22Service kc22Service;
    private final Kc24Service kc24Service;
    private final Kc08Service kc08Service;

    /**
     * 没有使用
     * @param criteria
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/orgZhsq")
    @ApiOperation("机构查询照护申请")
    public ResponseEntity<Object> orgZhsq(GeneralQueryCriteria criteria,
                                                       @RequestParam(defaultValue = "0")Integer page,
                                                       @RequestParam(defaultValue = "10")Integer size) {
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        criteria.setDdbm(gsgl.getBisGsglYljgdm());

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }


    @GetMapping(value = "/centerQueryOrgAssessZhsqxx")
    @ApiOperation("中心查询机构的待评估照护申请")
    public ResponseEntity<Object> centerQueryOrgAssessZhsqxx(GeneralQueryCriteria criteria,
                                                       @RequestParam(defaultValue = "0")Integer page,
                                                       @RequestParam(defaultValue = "10")Integer size) {
        criteria.setUserId(SecurityUtils.getCurrentUserId());
        criteria.setZt("54");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQueryOrgCheckZhsqxx")
    @ApiOperation("中心查询机构的待复核照护申请")
    public ResponseEntity<Object> centerQueryOrgCheckZhsqxx(GeneralQueryCriteria criteria,
                                                             @RequestParam(defaultValue = "0")Integer page,
                                                             @RequestParam(defaultValue = "10")Integer size) {
        criteria.setUserId(SecurityUtils.getCurrentUserId());
        criteria.setZt("55");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }


    @GetMapping(value = "/centerQueryOrgSelfAssessment")
    @ApiOperation("中心查询机构的审评审批")
    public ResponseEntity<Object> centerQueryOrgSelfAssessment(GeneralQueryCriteria criteria,
                                                             @RequestParam(defaultValue = "0")Integer page,
                                                             @RequestParam(defaultValue = "10")Integer size) {
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONObject deptJson = new JSONObject(userJson.get("dept"));

        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb03 roleString:{}", roleString);
        if(roleString.indexOf("中心人员") != -1) {
            criteria.setUserId(SecurityUtils.getCurrentUserId());
        } else {
            criteria.setTcq(String.valueOf(deptJson.get("id")));
        }

        criteria.setZt("11");
        criteria.setZhlb("01");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQueryHomeSelfAssessment")
    @ApiOperation("中心查询居家的审评审批")
    public ResponseEntity<Object> centerQueryHomeSelfAssessment(GeneralQueryCriteria criteria,
                                                            @RequestParam(defaultValue = "0")Integer page,
                                                            @RequestParam(defaultValue = "10")Integer size) {
        criteria.setUserId(SecurityUtils.getCurrentUserId());
        criteria.setZt("11");
        criteria.setZhlb("03");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQueryOrgZhsqxx")
    @ApiOperation("中心查询所有机构的照护申请")
    public ResponseEntity<Object> centerQueryOrgZhsqxx(GeneralQueryCriteria criteria,
                                                            @RequestParam(defaultValue = "0")Integer page,
                                                            @RequestParam(defaultValue = "10")Integer size) {
        criteria.setZhlb("01");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQueryHomeZhsqxx")
    @ApiOperation("中心查询居家的照护申请")
    public ResponseEntity<Object> centerQueryHomeZhsqxx(GeneralQueryCriteria criteria,
                                                              @RequestParam(defaultValue = "0")Integer page,
                                                              @RequestParam(defaultValue = "10")Integer size) {
        criteria.setZhlb("03");

        int[] startEnd = PageUtil.transToStartEnd(page, size);
/*

        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setSbgsId(gsgl.getBisGsglId());
        }
*/

        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        if(roleString.indexOf("居家照护") != -1  || roleString.indexOf("居家机构双角色") != -1 ) {
            log.info("角色：居家照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setDdbm(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQuerOrgZhsqxxIn220101")
    @ApiOperation("中心查询市级统筹机构的照护申请")
    public ResponseEntity<Object> centerQuerOrgZhsqxxIn220101(GeneralQueryCriteria criteria,
                                                              @RequestParam(defaultValue = "0")Integer page,
                                                              @RequestParam(defaultValue = "10")Integer size) {
        criteria.setTcq("220101");
        criteria.setZhlb("01");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryCommonZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQuerOrgZhsqxxNotIn220101")
    @ApiOperation("中心查询外县统筹机构的照护申请")
    public ResponseEntity<Object> centerQuerOrgZhsqxxNotIn220101(GeneralQueryCriteria criteria,
                                                              @RequestParam(defaultValue = "0")Integer page,
                                                              @RequestParam(defaultValue = "10")Integer size) {
        criteria.setNotInCenter("true");
        criteria.setZhlb("01");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryCommonZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/centerQuerOrgZhsqxxInSbgs")
    @ApiOperation("中心查询商保公司管理的照护申请")
    public ResponseEntity<Object> centerQuerOrgZhsqxxInSbgs(GeneralQueryCriteria criteria,
                                                                 @RequestParam(defaultValue = "0")Integer page,
                                                                 @RequestParam(defaultValue = "10")Integer size) {


        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setSbgsId(gsgl.getBisGsglId());
        }

        criteria.setInSbgs("true");
        // criteria.setZhlb("01");
        if("03".equals(criteria.getZhlb())) {
            if("05".equals(criteria.getZt())) {
                criteria.setZt(null);
                List<String> list = new ArrayList<String>();
                list.add("05");
                list.add("06");
                list.add("07");
                list.add("12");
                list.add("13");
                list.add("90");
                criteria.setZtList(list);
                criteria.setInZt("true");
            }
        } else {
            if("05".equals(criteria.getZt())) {
                criteria.setZt(null);
                List<String> list = new ArrayList<String>();
                list.add("05");
                list.add("90");
                criteria.setZtList(list);
                criteria.setInZt("true");
            }
        }

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryCommonZhxx(criteria, startEnd), HttpStatus.OK);
    }



    @Log("导出商保公司下的人员")
    @ApiOperation("导出商保公司下的人员")
    @GetMapping(value = "/centerQuerOrgZhsqxxInSbgs/download")
    public void exportSbDown(HttpServletResponse response, GeneralQueryCriteria criteria) throws IOException {

        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setSbgsId(gsgl.getBisGsglId());
        }

        criteria.setInSbgs("true");
        //criteria.setZhlb("01");
        if("03".equals(criteria.getZhlb())) {
            if("05".equals(criteria.getZt())) {
                criteria.setZt(null);
                List<String> list = new ArrayList<String>();
                list.add("05");
                list.add("06");
                list.add("07");
                list.add("12");
                list.add("13");
                list.add("90");
                criteria.setZtList(list);
                criteria.setInZt("true");
            }
        } else {
            if("05".equals(criteria.getZt())) {
                criteria.setZt(null);
                List<String> list = new ArrayList<String>();
                list.add("05");
                list.add("90");
                criteria.setZtList(list);
                criteria.setInZt("true");
            }
        }

        generalQueryService.queryCommonZhxxDown(criteria, response);
    }


    @GetMapping(value = "/outQueryOrgAssessZhsqxx")
    @ApiOperation("外县查询机构的待评估照护申请")
    public ResponseEntity<Object> outQueryOrgAssessZhsqxx(GeneralQueryCriteria criteria,
                                                             @RequestParam(defaultValue = "0")Integer page,
                                                             @RequestParam(defaultValue = "10")Integer size) {
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        criteria.setBisGsglSsqxCode(String.valueOf(deptJson.get("id")));
        criteria.setZt("54");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/outQueryOrgCheckZhsqxx")
    @ApiOperation("外县查询机构的待复核照护申请")
    public ResponseEntity<Object> outQueryOrgCheckZhsqxx(GeneralQueryCriteria criteria,
                                                          @RequestParam(defaultValue = "0")Integer page,
                                                          @RequestParam(defaultValue = "10")Integer size) {
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        System.out.println(userJson);
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        criteria.setBisGsglSsqxCode(String.valueOf(deptJson.get("id")));
        criteria.setZt("55");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @GetMapping(value = "/outQueryOrgZhsqxx")
    @ApiOperation("外县查询所有机构的照护申请")
    public ResponseEntity<Object> outQueryOrgZhsqxx(GeneralQueryCriteria criteria,
                                                       @RequestParam(defaultValue = "0")Integer page,
                                                       @RequestParam(defaultValue = "10")Integer size) {
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        System.out.println(userJson);
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        criteria.setTcq(String.valueOf(deptJson.get("id")));
        criteria.setZhlb("01");

        int[] startEnd = PageUtil.transToStartEnd(page, size);

        return new ResponseEntity<>(generalQueryService.queryZhxx(criteria, startEnd), HttpStatus.OK);
    }

    @ApiOperation("拨付单")
    @GetMapping(value = "/queryKb03")
    public ResponseEntity<Object> queryKb03(PaidQueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb03 roleString:{}", roleString);
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(unPaidService.queryKb03(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("拨付单打印")
    @GetMapping(value = "/queryPrintKb03")
    public ResponseEntity<Object> queryPrintKb03(@RequestParam String akb020, @RequestParam String aae002, @RequestParam String insu_no, @RequestParam String cjt100, @RequestParam String aab034) {
        return new ResponseEntity<>(unPaidService.queryKb03(akb020, aae002, insu_no, cjt100, aab034), HttpStatus.OK);
    }

    @ApiOperation("年拨付单打印")
    @GetMapping(value = "/queryPrintKb04")
    public ResponseEntity<Object> queryPrintKb04(@RequestParam String akb020, @RequestParam String aae001, @RequestParam String insu_no, @RequestParam String cjt100, @RequestParam String aae140) {

        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            insu_no = gsgl.getBisGsglYbqh();
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            akb020 = gsgl.getBisGsglYljgdm();
        }


        return new ResponseEntity<>(unPaidService.queryKb04(akb020, aae001, insu_no, cjt100, aae140), HttpStatus.OK);
    }

    @Log("导出拨付单")
    @ApiOperation("导出拨付单")
    @GetMapping(value = "/queryKb03/download")
    public void exportKb03(HttpServletResponse response, PaidQueryCriteria criteria) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        sbgsService.downloadKb03(sbgsService.queryKb03(criteria), response);
    }

    @ApiOperation("拨付单明细")
    @GetMapping(value = "/queryKb22")
    public ResponseEntity<Object> queryKb22(PaidQueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb22 roleString:{}", roleString);
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(unPaidService.queryKb22(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出拨付单明细")
    @ApiOperation("导出拨付单明细")
    @GetMapping(value = "/queryKb22/download")
    public void exportKb22(HttpServletResponse response, PaidQueryCriteria criteria) throws IOException {

        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        sbgsService.downloadKb22(sbgsService.queryKb22(criteria), response);
    }


    @ApiOperation("年度拨付表")
    @GetMapping(value = "/queryKb04")
    public ResponseEntity<Object> queryKb04(Kb04QueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(kb04Service.queryKB04PageList(criteria, pageable), HttpStatus.OK);
    }
    @ApiOperation("年度拨付表")
    @GetMapping(value = "/queryNewKb04")
    public ResponseEntity<Object> queryNewKb04(Kb04QueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(kb04Service.queryKB04NewPageList(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出年度拨付")
    @ApiOperation("导出年度拨付")
    @GetMapping(value = "/queryNewKb04/download")
    public void exportNewKb04(HttpServletResponse response, Kb04QueryCriteria criteria) throws IOException {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKb04 roleString:{}", roleString);
        if(roleString.indexOf("商保公司") != -1) {
            log.info("角色：商保公司");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setInsuNo(gsgl.getBisGsglYbqh());
        } else if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("拨付单") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        kb04Service.downloadKB04NewList(criteria, response);
    }


    @ApiOperation("查询历史就诊登记")
    @GetMapping(value = "/queryKc21")
    public ResponseEntity<Object> queryKc21(Kc21QueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKc21 roleString:{}", roleString);
        if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }

        return new ResponseEntity<>(kc21Service.queryKc21VoPageList(criteria, pageable), HttpStatus.OK);
    }


    @ApiOperation("查询费用明细")
    @GetMapping(value = "/queryKc22")
    public ResponseEntity<Object> queryKc22(Kc22QueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kc22Service.queryKc22VoPageList(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("查询结算信息")
    @GetMapping(value = "/queryKc24")
    public ResponseEntity<Object> queryKc24(Kc24QueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(kc24Service.queryKc24VoPageList(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("查询结算信息")
    @GetMapping(value = "/queryKc244")
    public ResponseEntity<Object> queryKc244(Kc24QueryCriteria criteria, Pageable pageable) {
        Long userid = SecurityUtils.getCurrentUserId();
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKc21 roleString:{}", roleString);
        if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            criteria.setAkb020(gsgl.getBisGsglYljgdm());
        }
        return new ResponseEntity<>(kc24Service.queryKc24VoPageList(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("封存信息")
    @GetMapping(value = "/queryKc08")
    public ResponseEntity<Object> queryKc08(Kc08QueryCriteria criteria,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size) {
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(kc08Service.queryKc08VoPageList(criteria, startEnd), HttpStatus.OK);
    }

    @ApiOperation("费用明细")
    @GetMapping(value = "/querydetailFee")
    public ResponseEntity<Object> querydetailFee(Kc08QueryCriteria criteria,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size) {
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(kc08Service.querydetailFee(criteria, startEnd), HttpStatus.OK);
    }

    @ApiOperation("人员信息")
    @GetMapping(value = "/queryRyxx")
    public ResponseEntity<Object> queryRyxx(Kc08QueryCriteria criteria,
                                                 @RequestParam(defaultValue = "0")Integer page,
                                                 @RequestParam(defaultValue = "10")Integer size) {
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(kc08Service.queryRyxx(criteria, startEnd), HttpStatus.OK);
    }

    @ApiOperation("照护保险申请信息")
    @GetMapping(value = "/queryZhbxxx")
    public ResponseEntity<Object> queryZhbxxx(Kc08QueryCriteria criteria,
                                            @RequestParam(defaultValue = "0")Integer page,
                                            @RequestParam(defaultValue = "10")Integer size) {
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(kc08Service.queryZhbxxx(criteria, startEnd), HttpStatus.OK);
    }



    @ApiOperation("查询外县参保人员在长发生的费用")
    @GetMapping(value = "/queryWxKb22")
    public ResponseEntity<Object> queryWxKb22(PaidQueryCriteria criteria,
                                              @RequestParam(defaultValue = "0")Integer page,
                                              @RequestParam(defaultValue = "10")Integer size) {
        int[] startEnd = PageUtil.transToStartEnd(page, size);

        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        log.info("queryWxKb22 deptJson:{}", deptJson);

        criteria.setAab034(deptJson.getStr("id"));

        return new ResponseEntity<>(unPaidService.queryKb221(criteria.getAkb020(), criteria.getAae002(), criteria.getInsuNo(), criteria.getCjt100(), criteria.getAab034() , startEnd), HttpStatus.OK);
    }

    @Log("查询外县参保人员在长发生的费用")
    @ApiOperation("查询外县参保人员在长发生的费用")
    @GetMapping(value = "/queryWxKb22/download")
    public void exportWxKb22(HttpServletResponse response, PaidQueryCriteria criteria) throws IOException {

        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        JSONObject deptJson = new JSONObject(userJson.get("dept"));
        log.info("queryWxKb22 deptJson:{}", deptJson);

        criteria.setAab034(deptJson.getStr("id"));

        unPaidService.queryKb221Down(criteria.getAkb020(), criteria.getAae002(), criteria.getInsuNo(), criteria.getCjt100(), criteria.getAab034(), response);
    }

    @GetMapping(value = "/queryFwmxcs")
    @ApiOperation("服务项目次数")
    public ResponseEntity<Object> queryFwmxcs(GeneralQueryCriteria criteria) {
        return new ResponseEntity<>(generalQueryService.queryFwmxcs(criteria), HttpStatus.OK);
    }
}
